<?php
require_once "../config.php";
//if(isset($_POST['mybut'])){  
    define ("MAX_SIZE","400");
    $extra=new Extra();
    $errors=0;
    $path = "../images/";
    $offer_id=$_GET['oid'];
    
    $image =$_FILES["file"]["name"];
    $uploadedfile = $_FILES['file']['tmp_name'];    
 echo $uploadedfile;
    if ($image) 
    {
        $filename = stripslashes($_FILES['file']['name']);

        $extension = $extra->getExtension($filename);
        $extension = strtolower($extension);		
		
        if (!in_array($extension, array("jpg", "jpeg", "png", "gif", "bmp"))){
            $change='<div class="msgdiv">Unknown Image extension </div> ';
            $errors=1;
        }else{
            $size=filesize($_FILES['file']['tmp_name']);
            if ($size > MAX_SIZE*1024)
            {
                $change='<div class="msgdiv">You have exceeded the size limit!</div> ';
                $errors=1;
            }  else {               
                //$tmp = $_FILES['photoimg']['tmp_name'];
                list($txt, $ext) = explode(".", $filename);
                $actual_image_name=time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
                if(move_uploaded_file($uploadedfile, $path.$actual_image_name)){
                    /*if($extension=="jpg" || $extension=="jpeg" ){
                        echo $uploadedfile;
                        $uploadedfile1 = $_FILES['file']['tmp_name'];
                        $src = imagecreatefromjpeg('C:\Users\Rukshan\Downloads\gb.jpeg');
                    }
                    else if($extension=="png"){
                        $uploadedfile1 = $_FILES['file']['tmp_name'];
                        $src = imagecreatefrompng($uploadedfile1);
                    }
                    else{
                        $src = imagecreatefromgif($uploadedfile);
                    }

                    list($width,$height)=getimagesize($uploadedfile);
                    $newwidth=120;
                    $newheight=($height/$width)*$newwidth;
                    $tmp=imagecreatetruecolor($newwidth,$newheight);
                    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
                    $filename1 = "../images/hotel/thumbs/". $_FILES['file']['name'];            
                    imagejpeg($tmp,$filename1,100);*/
                    
                    $offer_obj = new Offer();
                    $offer_image_array=array("offer_id"=>$offer_id,
                                            "image_name"=>$actual_image_name,
                                            "is_default"=>0);
                    $offer_obj->create_offer_images($offer_image_array);
                    echo "<img src='".$path.$actual_image_name."'  class='preview'>";
                }else{
                    $change="failed";
                    $errors=1;
                }
            /*if($extension=="jpg" || $extension=="jpeg" )
            {
                $uploadedfile = $_FILES['file']['tmp_name'];
                $src = imagecreatefromjpeg($uploadedfile);
            }
            else if($extension=="png")
            {
                $uploadedfile = $_FILES['file']['tmp_name'];
                $src = imagecreatefrompng($uploadedfile);
            }
            else 
            {
                $src = imagecreatefromgif($uploadedfile);
            }

            echo $src;
            list($width,$height)=getimagesize($uploadedfile);

            $newwidth=60;
            $newheight=($height/$width)*$newwidth;
            $tmp=imagecreatetruecolor($newwidth,$newheight);

            $newwidth1=25;
            $newheight1=($height/$width)*$newwidth1;
            $tmp1=imagecreatetruecolor($newwidth1,$newheight1);

            imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
            imagecopyresampled($tmp1,$src,0,0,0,0,$newwidth1,$newheight1,$width,$height);

            $filename = "../images/". $_FILES['file']['name'];
            $filename1 = "../images/small". $_FILES['file']['name'];
            
            imagejpeg($tmp,$filename,100);
            imagejpeg($tmp1,$filename1,100);

            imagedestroy($src);
            imagedestroy($tmp);
            imagedestroy($tmp1);
            }*/
            
            }
            
            }
            
            }  else {
                $change="Please select an image";
                $errors=1;
            }
            if ($errors) {
                echo $change;
            }
            /*
            if (!$errors){
                $return_arr["success"]= true;
                $return_arr["err_msg"]= "Successfully uploaded";
                $return_arr["image1"]= $filename;
                $return_arr["image2"]= $filename1;
                echo json_encode($return_arr);	                
            }  else {                
                $return_arr["success"]= false;  
                $return_arr["err_msg"]= $change;
                echo json_encode($return_arr);                
            } */
/*}  else {
    echo 'Invalid access';
}*/
/*$return_arr["success"]= false;
$return_arr["err_msg"]= "Invalid access";
echo json_encode($return_arr);       */ 